package com.coszero.utils.utils

import android.graphics.Color

/**
 * Desc： 颜色渐变类
 *
 *
 * @author ： xmqian
 * Email:xmqian93@163.com
 * Date: 2020/4/13 17:42
 * titleColorShades.setFromColor(0xffffffff)
 * .setToColor(0xffff00ff)
 * .setShade(decimal.floatValue());
 *
 * @line(https://gist.github.com/cooltechworks/4f37021b1216f773daf8) Source from :
 * https://gist.github.com/cooltechworks/4f37021b1216f773daf8
 * Color shades will provide all the intermediate colors between two colors. It just requires a decimal value between
 * 0.0 to 1.0
 * and it provides the exact shade combination of the two color with this shade value.
 * Textual explanation :
 *
 *
 * White          LtGray          Gray            DkGray           Black
 * 0               0.25            0.5             0.75            1
 * Given two colors as White and Black,and shade
 * as 0    gives White
 * as 0.25 gives Light gray
 * as 0.5  gives Gray
 * as 0.75 gives Dark gray
 * as 1    gives Black.
 *
 * @version 1
 */
class ColorShades {
    private var mFromColor: Int = 0
    private var mToColor: Int = 0
    private var mShade: Float = 0f

    /**
     * 开始颜色色值
     *
     * @param fromColor int色值
     * @return 返回自己
     */
    fun setFromColor(fromColor: Int): ColorShades {
        this.mFromColor = fromColor
        return this
    }

    /**
     * 改变的最终色值
     *
     * @param toColor int色值
     * @return 返回自己
     */
    fun setToColor(toColor: Int): ColorShades {
        this.mToColor = toColor
        return this
    }

    /**
     * 设置开始色值
     *
     * @param fromColor string 色值 #FFFFFF
     * @return 返回自己
     */
    fun setFromColor(fromColor: String?): ColorShades {
        this.mFromColor = Color.parseColor(fromColor)
        return this
    }

    /**
     * 设置最终色值
     *
     * @param toColor string色值 #FFFFFF
     * @return 返回自己
     */
    fun setToColor(toColor: String?): ColorShades {
        this.mToColor = Color.parseColor(toColor)
        return this
    }

    /**
     * 从白色需要渐变的颜色
     *
     * @param color 最终改变的颜色
     * @return 返回自己
     */
    fun forLightShade(color: Int): ColorShades {
        setFromColor(Color.WHITE)
        setToColor(color)
        return this
    }

    /**
     * 从任意颜色转变到黑色
     *
     * @param color 开始色值
     * @return 返回自己
     */
    fun forDarkShare(color: Int): ColorShades {
        setFromColor(color)
        setToColor(Color.BLACK)
        return this
    }

    /**
     * 设置渐变比例
     *
     * @param shade 比率 0.1f
     * @return 返回自己
     */
    fun setShade(shade: Float): ColorShades {
        //增加传入异常的数值判断
        if (shade > 1) {
            this.mShade = 1f
            return this
        }
        if (shade < 0) {
            this.mShade = 0f
            return this
        }
        this.mShade = shade
        return this
    }

    /**
     * 生成渐变色色值
     *
     * @return 返回int值的渐变色
     */
    fun generate(): Int {
        val fromR: Int = Color.red(mFromColor)
        val fromG: Int = Color.green(mFromColor)
        val fromB: Int = Color.blue(mFromColor)

        val toR: Int = Color.red(mToColor)
        val toG: Int = Color.green(mToColor)
        val toB: Int = Color.blue(mToColor)

        val diffR: Int = toR - fromR
        val diffG: Int = toG - fromG
        val diffB: Int = toB - fromB

        val red: Int = fromR + ((diffR * mShade)).toInt()
        val green: Int = fromG + ((diffG * mShade)).toInt()
        val blue: Int = fromB + ((diffB * mShade)).toInt()

        return Color.rgb(red, green, blue)
    }

    /**
     * 反向渐变色，比如设置的是由黑变白，使用此方法是由白变黑，及100%为黑色
     *
     * @return 返回色值
     */
    fun generateInverted(): Int {
        val fromR: Int = Color.red(mFromColor)
        val fromG: Int = Color.green(mFromColor)
        val fromB: Int = Color.blue(mFromColor)

        val toR: Int = Color.red(mToColor)
        val toG: Int = Color.green(mToColor)
        val toB: Int = Color.blue(mToColor)

        val diffR: Int = toR - fromR
        val diffG: Int = toG - fromG
        val diffB: Int = toB - fromB

        val red: Int = toR - ((diffR * mShade)).toInt()
        val green: Int = toG - ((diffG * mShade)).toInt()
        val blue: Int = toB - ((diffB * mShade)).toInt()
        return Color.rgb(red, green, blue)
    }

    /**
     * Gets the String equivalent of the generated shade
     *
     * @return 返回字符串的颜色 #FFFFFF
     */
    fun generateInvertedString(): String {
        return String.format("#%06X", 0xFFFFFF and generateInverted())
    }

    /**
     * Gets the inverted String equivalent of the generated shade
     *
     * @return 返回反向字符串的颜色 #FFFFFF
     */
    fun generateString(): String {
        return String.format("#%06X", 0xFFFFFF and generate())
    }
}
